﻿Imports System.Text.RegularExpressions
Imports System.Collections.Specialized

Public Class EnglistTestWord2
    Private cls(36) As Wys.Control.Vb.WordTest

#Region "只能入力数字 "
    Private Sub txtPage_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
        '应许输入退格键 C# 强制转换
        If Asc(e.KeyChar) <> 8 Then
            '正则验证数字
            If (Not Regex.IsMatch(e.KeyChar.ToString(), "^[0-9]$")) Then
                e.Handled = True
                Return
            End If
        End If
    End Sub
#End Region

#Region "页数"
    Private _TotalPage As Integer = 0
    Public Property TotalPage() As Integer
        Get
            Return _TotalPage
        End Get
        Set(ByVal value As Integer)
            _TotalPage = value
        End Set
    End Property

    Private _CurrentPage As Integer = 0
    Public Property CurrentPage() As Integer
        Get
            Return _CurrentPage
        End Get
        Set(ByVal value As Integer)
            _CurrentPage = value
        End Set
    End Property
#End Region

#Region "设置记录条数"
    Private _CurrentTable As DataTable = Nothing
#End Region

    Private Sub EnglistTestWord_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ReDim cls(Me.Get窗体行数())

        For i As Integer = 1 To (cls.Length) / 3
            For j As Integer = 1 To 3
                If (i - 1) * 3 + j > Me.Get窗体行数 Then
                    GoTo 退出
                End If
                cls((i - 1) * 3 + j - 1) = New Wys.Control.Vb.WordTest()
                cls((i - 1) * 3 + j - 1).Left = 8 + 241 * (j - 1) + 5 * (j - 1)
                cls((i - 1) * 3 + j - 1).Top = 101 + 29 * (i - 1) + 6 * (i - 1)
                cls((i - 1) * 3 + j - 1).ImeMode = Windows.Forms.ImeMode.Off
                cls((i - 1) * 3 + j - 1).单词 = ""
                cls((i - 1) * 3 + j - 1).单词意思 = ""
                cls((i - 1) * 3 + j - 1).单词意思数据库 = ""

                AddHandler cls((i - 1) * 3 + j - 1).RefreshTitle, AddressOf WordTest36_RefreshTitle
                Me.Controls.Add(cls((i - 1) * 3 + j - 1))
            Next
        Next

退出:


        Me.txt发音.Font = New Drawing.Font("Lucida Sans Unicode", Me.MyEdit1.Font.Size)
    End Sub

    Private Sub WordTest36_RefreshTitle(ByVal strTitle As System.Object, ByVal strFa As String)
        MyEdit1.Text = strTitle
        txt发音.Text = strFa
        RefrshState()
    End Sub

    Private Sub cbo页数_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)

        Dim strSql As String = "select ID,WORD,CHINESEWORD,SyombalAmerican from XSCENLISHWORD where id in(" & CStr(cbo页数.Tag) & ") ORDER BY OPTIME DESC Limit " + Me.Get窗体行数().ToString + " Offset " & ((CInt(cbo页数.Text) - 1) * Me.Get窗体行数()).ToString
        _CurrentTable = ct.FillTable(ct.GetTable(strSql))

        For i As Integer = 0 To cls.Length - 1
            If _CurrentTable.Rows.Count > 0 Then
                cls(i).单词意思 = Convert.ToString(_CurrentTable.Rows(0)("CHINESEWORD"))
                cls(i).单词 = Convert.ToString(_CurrentTable.Rows(0)("WORD"))
                cls(i).单词发音 = Convert.ToString(_CurrentTable.Rows(0)("SyombalAmerican"))
                _CurrentTable.Rows.RemoveAt(0)
            Else
                Exit For
            End If
        Next
        cls(0).Focus()
    End Sub

    Public Sub RefrshState()
        Dim count As Integer = 0
        For Each c2 As Wys.Control.Vb.WordTest In cls
            If c2 IsNot Nothing Then
                If c2.选中状态 Then
                    count = count + 1
                End If
            End If
        Next
        Me.txt状态.Text = "正确:" + count.ToString + "/共:" & Me.Get窗体行数().ToString
    End Sub

    Private _开始学习 As Boolean = False
    Private Sub btn开始学习_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn开始学习.Click

        If btn开始学习.Text = "开始学习" Then
            btn开始学习.Text = "停止学习"
            _开始学习 = True
            For Each c2 As Wys.Control.Vb.WordTest In cls
                If _开始学习 Then
                    If c2 IsNot Nothing Then
                        If c2.单词意思数据库.Trim <> "" Then
                            c2.Focus()
                            朗读单词(c2.单词意思数据库)
                            System.Threading.Thread.Sleep(500)
                            System.Windows.Forms.Application.DoEvents()
                            朗读单词(c2.单词意思数据库)
                            System.Threading.Thread.Sleep(500)
                            System.Windows.Forms.Application.DoEvents()
                            c2.单词意思数据库 = ""
                            System.Threading.Thread.Sleep(500)
                            System.Windows.Forms.Application.DoEvents()
                        Else
                            Exit For
                        End If
                    Else
                        Exit For
                    End If
                End If
            Next
            btn开始学习.Text = "开始学习"
            _开始学习 = False
        Else
            btn开始学习.Text = "开始学习"
            _开始学习 = False
        End If
    End Sub

    Private _开始浏览 As Boolean = False
    Private Sub btn开始浏览_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn开始浏览.Click
        If btn开始浏览.Text = "开始浏览" Then
            btn开始浏览.Text = "停止浏览"
            _开始浏览 = True
            For Each c2 As Wys.Control.Vb.WordTest In cls
                If _开始浏览 Then
                    If c2 IsNot Nothing Then
                        If c2.单词意思数据库.Trim <> "" Then
                            c2.Focus()
                            c2.单词意思数据库 = c2.单词意思数据库
                            System.Windows.Forms.Application.DoEvents()
                            朗读单词(c2.单词意思数据库)
                            System.Threading.Thread.Sleep(500)
                            System.Windows.Forms.Application.DoEvents()
                            朗读单词(c2.单词意思数据库)
                            System.Threading.Thread.Sleep(500)
                            System.Windows.Forms.Application.DoEvents()
                            System.Threading.Thread.Sleep(500)
                            System.Windows.Forms.Application.DoEvents()
                        Else
                            Exit For
                        End If
                    Else
                        Exit For
                    End If
                End If
            Next
            btn开始浏览.Text = "开始浏览"
            _开始浏览 = False
        Else
            btn开始浏览.Text = "开始浏览"
            _开始浏览 = False
        End If
    End Sub

    Public Sub 朗读单词(ByVal strWord As String)
        Dim strSQL As String = "select * from XSCWORDVoice where WORD='" + strWord + "'"
        Dim rs As ADODB.Recordset = ct.ExecuteSql(strSQL)
        If rs IsNot Nothing AndAlso Not rs.EOF Then
            UTIL.播放类.Play(rs.Fields("WORDPATH").Value.ToString())
            rs.Close()
        End If
        System.Runtime.InteropServices.Marshal.ReleaseComObject(rs)
    End Sub

#Region "重载设置窗体函数 A-2010-09-02"
    Public Overrides Function 初始行数() As Integer
        Return 20
    End Function

    Public Overloads Function Get窗体行数() As Integer
        Return MyBase.Get窗体行数(初始行数())
    End Function

    Public Overrides Function 是否显示设置窗体数据行数() As Boolean
        Return True
    End Function
#End Region
    Dim 当前英语备注ID As Integer = 0
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        ClearForm()

        Using frm As Wys.Control.Vb.ListForm = New Wys.Control.Vb.ListForm
            frm.strName = Me.strName
            frm.iType = Me.iType
            frm.SQL = "select * from MEMO where type=1"
            Dim 显示列 As New StringCollection
            显示列.Add("TITLE")
            frm.显示列 = 显示列
            frm.ShowDialog()
            If frm.返回值.Count > 0 Then
                当前英语备注ID = CInt(frm.返回值(0))
                txt备注.Text = CStr(frm.返回值(1))
            Else
                当前英语备注ID = 0
                txt备注.Text = ""
            End If
        End Using

        cbo页数.Text = ""

        Dim strsq2 As String = ""
        If 当前英语备注ID > 0 Then
            strsq2 = " Where MemoID=" & 当前英语备注ID
        End If
        Dim rs As ADODB.Recordset = ct.GetTable("select count(*) as PageCount from XSCENLISHWORD where WORD in(select EnglishWord from EnglishMEMO " & strsq2 & ")")
        TotalPage = System.Math.Ceiling(rs.Fields("PageCount").Value / Me.Get窗体行数())
        rs.Close()
        System.Runtime.InteropServices.Marshal.ReleaseComObject(rs) '20100531
        rs = Nothing
        cbo页数.Items.Clear()
        For idex As Integer = 1 To TotalPage
            cbo页数.Items.Add(idex.ToString())
        Next

        If TotalPage = 0 Then
            cbo页数.Items.Add("1")
        End If

    End Sub

    Public Sub ClearForm()
        For i As Integer = 0 To cls.Length - 1
            Try
                cls(i).单词 = ""
                cls(i).单词意思 = ""
            Catch ex As Exception

            End Try
        Next
    End Sub

    Private Sub cbo页数_SelectedIndexChanged_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbo页数.SelectedIndexChanged

        ClearForm()

        Dim strsq2 As String = ""
        If 当前英语备注ID > 0 Then
            strsq2 = " Where MemoID=" & 当前英语备注ID
        End If
        Dim strSql As String = "select ID,WORD,CHINESEWORD,SyombalAmerican from XSCENLISHWORD where WORD in(select EnglishWord from EnglishMEMO" & strsq2 & ") ORDER BY OPTIME DESC Limit " + Me.Get窗体行数().ToString + " Offset " & ((CInt(cbo页数.Text) - 1) * Me.Get窗体行数()).ToString
        _CurrentTable = ct.FillTable(ct.GetTable(strSql))

        For i As Integer = 0 To cls.Length - 1
            If _CurrentTable.Rows.Count > 0 Then
                cls(i).单词意思 = Convert.ToString(_CurrentTable.Rows(0)("CHINESEWORD"))
                cls(i).单词 = Convert.ToString(_CurrentTable.Rows(0)("WORD"))
                cls(i).单词发音 = Convert.ToString(_CurrentTable.Rows(0)("SyombalAmerican"))
                _CurrentTable.Rows.RemoveAt(0)
            Else
                Exit For
            End If
        Next
        cls(0).Focus()
    End Sub
End Class
